package com.zw.workflowllm.service;

import lombok.RequiredArgsConstructor;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class HolidayWorkflowService {

    private final RuntimeService runtimeService;
    private final TaskService taskService;

    /**
     * 启动请假流程
     *
     * @param employeeId 申请人ID
     * @param variables
     * @return
     */
    @Transactional
    public ProcessInstance startHolidayProcess(String employeeId, Map<String, Object> variables) {
        variables.put("employeeId", employeeId);
        return runtimeService.startProcessInstanceByKey("holiday-request2", variables);
    }

    /**
     * 获取当前用户任务列表
     *
     * @param assigneeId 待办人ID
     * @return
     */
    public List<Task> getTasks(String assigneeId) {
        return taskService.createTaskQuery()
                .taskAssignee(assigneeId)
                .orderByTaskCreateTime()
                .desc()
                .list();
    }

    /**
     * 完成任务
     *
     * @param taskId
     * @param variables
     */
    @Transactional
    public void completeTask(String taskId, Map<String, Object> variables) {
        taskService.complete(taskId, variables);
    }

    public Task getTaskById(String taskId) {
        return taskService.createTaskQuery().taskId(taskId).singleResult();
    }
}
